App.View = (function(lng, App) {

    var $ = lng.dom;
    var _element = App.Data.Element;
    var _onPageChange = [];
    var _sections = [];

    /**
     * 注册一个页面切换时的函数钩
     * @param {type} key
     * @param {type} func
     * @returns {undefined}
     */
    var registerPageChangeFunc = function(key, func) {
        _onPageChange[key] = func;
    };

    var showConfirm = function(content, first_label, first_callback, second_label, second_callback) {
        var notif = {
            title: '提示',
            description: content,
            accept: {
                color: 'blue',
                label: first_label,
                callback: first_callback
            },
            cancel: {
                color: 'blue',
                label: second_label,
                callback: second_callback
            }
        };
        lng.Notification.confirm(notif);
    };

    // remove section
    var pushCachePage = function(section) {
        section = section || lng.Element.Cache.section;
        if (section && section[0]) {
            var section_id = section[0].id;
            if (section_id) {
                _sections["#" + section_id] = section[0];
            }
            window.setTimeout(function() {
                section.remove();
            }, 200);
        }
    };

    // add section to body
    var pullCachePage = function(section_id) {
        var $_section = $("section" + section_id);
        if (!$_section || $_section.length === 0) {
            var section = _sections[section_id];
            $("body").append(section);
            _sections[section_id] = '';
        }
    };

    /**
     * 设置页面基础组件
     * @param {type} article_id
     * @returns {undefined}
     */
    var showPage = function(article_id) {
        var $_page = $('#' + article_id);
        var dom_page = $_page[0];
        if (dom_page) {
            var parent = $_page.parent();
            var header = $_page.attr(_element.DATA_HEADER);
            _setHeader(parent, header);
            var nav = $_page.attr(_element.DATA_NAV);
            _setNav(parent, nav);
            // 某些页面需要在切换时的函数钩
            for (var key in _onPageChange) {
                _onPageChange[key] && _onPageChange[key](article_id);
            }
        }
    };

    var _setHeader = function(parent, header) {
        var headers = parent.find('header');
        headers.hide();
        var current = headers.first();
        if (header) {
            current = parent.find('header#' + header);
        }
        current && current.show();
    };

    var _setNav = function(parent, nav) {
        var navs = parent.find('nav.hnav');
        _cacheNavs(navs);
        navs.remove();
        if (nav) {
            //var current = parent.find('nav.hnav#' + nav);
            var current = _hnavs[nav];
            if (current) {
                parent.append(current);
            }
        }
    };

    var _hnavs = [];
    var _cacheNavs = function(hnavs) {
        for (var i in hnavs) {
            var id = hnavs[i].id;
            _hnavs[id] = hnavs[i];
        }
    };

    return {
        showPage: showPage,
        registerPageChangeFunc: registerPageChangeFunc,
        showConfirm: showConfirm,
        pushCachePage: pushCachePage,
        pullCachePage: pullCachePage
    };
})(Lungo, App);

App.View.Helper = (function(lng) {

    var setTitle = function(title) {
        if (title.length > 10) {
            title = title.substring(0, 10) + '...';
        }
        lng.View.Article.title(title);
    };

    var createElement = function(content) {
        var div;
        div = document.createElement("div");
        div.innerHTML = content;
        return div.firstChild;
    };

    return {
        setTitle: setTitle,
        createElement: createElement
    };

})(Lungo);